package oj;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 郝文雪
 * @date 2022/8/23 18:34
 * @Description Test
 */
public class LC2131 {
    public int longestPalindrome(String[] words) {
        if(words==null || words.length==0) return 0;
        int len=0;
        String reverse="";
        Map<String,Integer> map=new HashMap<>();
        int[] hash=new int[26];

        for(String word:words){
            StringBuilder sb=new StringBuilder();
            reverse=sb.append(word).reverse().toString();
            if(reverse.equals(word)){
                hash[reverse.charAt(0)-'a']++;
            }else{
                if(map.get(reverse)!=null && map.get(reverse)>0){
                    len+=4;
                    map.put(reverse,map.get(reverse)-1);
                }else{
                    map.put(word,map.getOrDefault(word,0)+1);
                }
            }
        }
        boolean last=false;
        for(int count:hash){
            if(count%2==0){
                len+=2*count;
            }else{
                len+=2*(count-1);
                last=true;
            }
        }
        if(last){
            len+=2;
        }
        return len;
    }
}
